<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
        >导出
        </el-button>
      </el-col>

      <el-button type="primary" @click="goUpList">返回上一级</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productTypeList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" prop="productTypeId"/>
      <el-table-column label="名称" align="center" prop="typeName"/>
      <el-table-column label="分类级别" align="center" prop="typeRank"/>
      <el-table-column label="商品数量" align="center" prop="productTypeNum"/>
      <el-table-column label="数量单位" align="center" prop="typeUnit"/>
      <el-table-column label="导航栏" align="center" prop="isNavigation">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isNavigation"
            :active-value="0"
            :inactive-value="1"
            @change="updateNavigation(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="显示" align="center" prop="isShow">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShow"
            :active-value="0"
            :inactive-value="1"
            @change="updateNavigation(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="isSort"/>
      <el-table-column label="设置" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="plain" @click="getNextList(scope.row)" v-hasPermi="['system:post:edit']">
            查看下级
          </el-button>
          <el-button size="mini" type="plain" @click="handleDelete(scope.row)" v-hasPermi="['system:post:remove']">
            转移商品
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:post:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:post:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[3, 5, 7, 10]"
      @size-change="handleSizeChange"
      :page.sync="typeListRequest.pageNum"
      :limit.sync="typeListRequest.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="productType" :rules="rules" label-width="80px">
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="productType.typeName" placeholder="请输入类型名称"/>
        </el-form-item>
        <el-form-item label="上级分类" prop="parentId">
          <el-select v-model="productType.parentId" placeholder="请选择">
            <el-option
              v-for="item in firstList"
              :key="item.productTypeId"
              :label="item.typeName"
              :value="item.productTypeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类级别" prop="typeRank">
          <el-input v-model="productType.typeRank" placeholder="请输入分类级别"/>
        </el-form-item>
        <el-form-item label="商品数量" prop="productTypeNum">
          <el-input v-model="productType.productTypeNum" placeholder="请输入商品数量"/>
        </el-form-item>
        <el-form-item label="数量单位" prop="typeUnit">
          <el-input v-model="productType.typeUnit" placeholder="请输入数量单位"/>
        </el-form-item>
        <el-form-item label="是否显示在导航栏" prop="isNavigation">
          <el-radio v-model="productType.isNavigation" :label="0">是</el-radio>
          <el-radio v-model="productType.isNavigation" :label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-radio v-model="productType.isShow" :label="0">是</el-radio>
          <el-radio v-model="productType.isShow" :label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="排序" prop="isSort">
          <el-input v-model="productType.isSort" placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="分类图标" prop="typeIcon">
         <imageUpload v-model="productType.typeIcon" :limit="1"></imageUpload>

        </el-form-item>
        <el-form-item label="分类关键词" prop="typeKeyword">
          <el-input v-model="productType.typeKeyword" placeholder="请输入关键词"/>
        </el-form-item>
        <el-form-item label="分类描述" prop="typeDescription">
          <el-input v-model="productType.typeDescription" placeholder="请输入分类描述"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {
  addProductType, deleteProductType,
  getProductTypeList, getUpList,
  selectFirstList,
  updateProductType
} from "../../../api/product/productType/productType";
import ImageUpload from "../../../components/ImageUpload/index.vue";

export default {
  name: "ProductType",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
      //上传图片
      fileList: [],
      //一级分类下拉框
      firstList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品分类表格数据
      productTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      typeListRequest: {
        parentId: 0,
        pageNum: 1,
        pageSize: 5
      },
      // 表单参数
      productType: {},
      // 表单校验
      rules: {
        postName: [
          {required: true, message: "岗位名称不能为空", trigger: "blur"}
        ],
        postCode: [
          {required: true, message: "岗位编码不能为空", trigger: "blur"}
        ],
        postSort: [
          {required: true, message: "岗位顺序不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getFirstList();
  },
  comments:{
   imageUpload:ImageUpload
  },
  methods: {
    goUpList(){
      console.log(this.typeListRequest);
      getUpList(this.typeListRequest.parentId).then(response => {
        if (response.code != 200){
          this.$message.warning(response.msg)
          return
        }
        this.typeListRequest = response.data;
        getProductTypeList(this.typeListRequest).then(response => {
          this.productTypeList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        });
      });
    },
    updateNavigation(row) {
      console.log(row);
      if (row.productTypeId != undefined) {
        updateProductType(row).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        });
      }
    }, handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.typeListRequest.pageSize = val;
      console.log(this.typeListRequest);
      this.getList()
    },
    /** 查询商品分类列表 */
    getList() {
      this.loading = true;
      getProductTypeList(this.typeListRequest).then(response => {
        this.productTypeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    }, /** 查询下级商品分类列表 */
    getNextList(row) {
      this.typeListRequest.parentId = row.productTypeId
      this.loading = true;
      getProductTypeList(this.typeListRequest).then(response => {
        if(response.data.list.length<=0){
          this.$message.warning("该商品没有下级")
        }else {
          this.productTypeList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      });
    },
    /** 查询一级分类列表 */
    getFirstList() {
      this.loading = true;
      selectFirstList().then(response => {
        this.firstList = response.data;
        console.log(this.firstList);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // this.firstList = []
      this.productType = {
        productTypeId: 0,
        typeName: '',
        typeRank: '',
        productTypeNum: undefined,
        typeUnit: '',
        isNavigation: undefined,
        isShow: undefined,
        isSort: undefined,
        typeIcon: '',
        typeKeyword: '',
        typeDescription: undefined,
        parentId: 0,
        isDelete: undefined
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.productType = row;
      console.log(this.productType);
      this.open = true;
      this.title = "修改商品分类";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      this.open = true;
      this.title = "添加商品分类";
    },
    /** 提交按钮 */
    submitForm: function () {
      console.log(this.productType)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.productType.productTypeId != 0) {
            updateProductType(this.productType).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductType(this.productType).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.productTypeId || this.ids;
      this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function () {
        return deleteProductType(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.typeListRequest
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
